<?php

namespace App\Http\Controllers\Admin\Rbac;

use App\Http\Controllers\Controller;
use App\Models\Rbac\Menu;
use App\Models\Rbac\Role;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request): JsonResponse
    {
        $params = $request->only('name','cn_name','status');
        $res = Role::query()->when($params['name'] ?? null, function ($query, $value) {
            return $query->where('name', $value);
        })->when($params['cn_name'] ?? null, function ($query, $value) {
            return $query->where('cn_name', $value);
        })->paginate(request('pageSize',20));
        return apiResponse(data: $res);
//        $res = Role::getQueryData($params);
//        foreach ($res as $role){
//            $role->menu_ids = $role->menus->pluck('id')->toArray();
//            unset($role->menus);
//        }
//        return apiResponse(data: $res);
    }

    public function getOptions(): JsonResponse
    {
        $menus = Menu::query()->whereNull('parent_id')
            ->with('children')->get();
//        $roles = Role::query()->whereNull('parent_id')->get();
        $roles = Role::query()->get();
        return apiResponse(data: [
            'menus' => $menus,
            'roles' => $roles
        ]);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): JsonResponse
    {
        $params = $request->all();

        $role = Role::create([
            'name' => $params['name'],
            'cn_name' => $params['cn_name'],
            'note' => $params['cn_name'],
        ]);
        if (!empty($params['menu_ids'])){
//            $role->menus()->attach($params['menu_ids'],['created_at' => now()]);
            $menuNames = Menu::whereIn('id', $params['menu_ids'])->pluck('name')->toArray();
            $role->syncPermissions($menuNames);
        }

        return apiResponse(0, '新增成功',$role);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id): JsonResponse
    {
        $params = $request->all();
        $role = Role::query()->findOrFail($id);
        $role->update($params);
        if (!empty($params['menu_ids'])){
            $menuNames = Menu::whereIn('id', $params['menu_ids'])->pluck('name')->toArray();
            $role->syncPermissions($menuNames);
        }
//        $role->menus()->sync(array_fill_keys($params['menu_ids'] ?? [], ['updated_at' => now()]));
        return apiResponse(0,'更新成功',$role);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $ids): JsonResponse
    {
        Role::destroy(explode(',',$ids));
        return apiResponse(msg: '删除成功');
    }

    /**
     * 获取角色菜单id
     * @param  string  $id
     * @return JsonResponse
     */
    public function getRoleMenuId(string $id): JsonResponse
    {
        $role = Role::findOrFail($id);
        $menuIds = Menu::query()->whereIn('name', $role->permissions->pluck('name'))->pluck('id');
        return apiResponse(data: $menuIds);
    }

}
